﻿@using Kojarskiy.Core
@using Kojarskiy.Models
@model Kojarskiy.Models.SiteMap

@{
    ViewBag.Title = "Редактирование статической страницы";
    var context = new KojarskiyDbEntities();
}
           
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@Html.ActionLink("Администрирование", "Index", "Admin") | @Html.ActionLink("Страницы", "Index", "SiteMap") | @ViewBag.Title
<div style="clear: both;"></div>

<fieldset>
<legend>@ViewBag.Title</legend>

@using (Html.BeginForm("Edit", "SiteMap", FormMethod.Post, new { enctype = "multipart/form-data", role = "form" }))
{
        <div class="bg-danger">@Html.ValidationSummary(true)</div>

        @Html.HiddenFor(model => model.SiteMapId)
        @Html.HiddenFor(model => model.ParentId)
        @Html.HiddenFor(model => model.Order)
        @Html.HiddenFor(model => model.Controller)
        @Html.HiddenFor(model => model.Action)
        @Html.Hidden("returnUrl", ViewData["requestUrl"])
        
        <div class="form-group">
            <label>Название страницы</label>
            @Html.TextBox("PageName", Model.PageName, new { @class = "form-control" })
            <div class="bg-danger">@Html.ValidationMessageFor(model => model.PageName)</div>
        </div>
        
        if(Model.ParentId != null)
        {
            
            <div class="form-group">
                <label>Путь страницы</label>
                @Html.TextBox("Path", Model.Path, new { @class = "form-control" })
                <div class="bg-danger">@Html.ValidationMessageFor(model => model.Path)</div>
            </div> 

            <div class="form-group">
                <label>Контент страницы</label>
                @Html.TextArea("Content", Model.Content, new { @class = "form-control multi-line" })
                <div class="bg-danger">@Html.ValidationMessageFor(model => model.Content)</div>
            </div>
    
           
            @Html.Partial("_BlockEditor", Model, new ViewDataDictionary() { new KeyValuePair<string, object>("Blocks", Model.Blocks.FirstOrDefault()) })

        }
        else
        {
            <div class="form-group">
                <label>Контент страницы</label>
                @Html.TextArea("Content", Model.Content, new { @class = "form-control multi-line" })
                <div class="bg-danger">@Html.ValidationMessageFor(model => model.Content)</div>
            </div>
                
            <fieldset>
                <legend>Изображения слайдера</legend>
                
                @foreach(var image in context.Gallery.Where(x => x.SiteMapId == 1))
                {
                    <div class="form-group" style="text-align: center; float: left; padding-right: 5px;">
                        <img src="@Url.Content(image.ThumbPath)" style="max-width: 200px; border: 1px solid gray;"/><br/>
                        <input type="text" id="imagedesc-@image.GalleryId" placeholder="Описание" value="@image.Description" name="imagedesc-@image.GalleryId" class="form-control" />
                        <input type="text" id="url-@image.GalleryId" name="url-@image.GalleryId" value="@image.Url" placeholder="Ссылка" class="form-control" />
                        <input type="button" onclick="DeleteSliderImage(@image.GalleryId)" value="Удалить" class="btn btn-danger btn-sm" style="margin-top: 5px;"/>
                    </div>
                }
                <p style="clear: both;"></p>
                <small>для выбора нескольких файлов укажите их удерживая Ctrl. Размер изображения: 1200x300</small>
                <input type="file" id="fileinput" name="files" multiple class="btn btn-default" />
            </fieldset>  
            <p></p>          
        }
        <div class="form-group">
            <label>Title страницы</label>
            @Html.TextBox("Title", Model.Title, new { @class = "form-control" })
            <div class="bg-danger">@Html.ValidationMessageFor(model => model.Title)</div>
        </div>    
    
        <div class="form-group">
            <label>Description страницы</label>
            @Html.TextBox("Description", Model.Description, new { @class = "form-control" })
            <div class="bg-danger">@Html.ValidationMessageFor(model => model.Description)</div>
        </div> 

        <div class="form-group">
            <label>Keywords страницы</label>
            @Html.TextBox("Keywords", Model.Keywords, new { @class = "form-control" })
            <div class="bg-danger">@Html.ValidationMessageFor(model => model.Keywords)</div>
        </div> 
    
        <div class="checkbox">
            <label>
                @Html.CheckBox("Show", Model.Show) Отображать страницу
            </label>
        </div>

        <div class="form-actions">
            <button type="submit" class="btn btn-success">Изменить</button>   
        </div>                          
}
        
</fieldset> 

<script type="text/javascript">
    
    $(document).ready(function () 
    {
        CKEDITOR.replace('Content', { 
            filebrowserUploadUrl: '/home/UploadImageFromEditor'});
    });

    function DeleteSliderImage(galleryId) {
        $.ajax({
            type: "POST",
            url: "/SiteMap/DeleteSliderImage",
            data: { galleryId: galleryId },
            success: location.reload()
        });
    }
</script>